 <!-- 商家销量统计的柱状图 -->
<template>
  <div class="com-container">
    <div class="con-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  data() {
    return {
      chartInstance: null,
      allData: [],
      startValue: 0, //区域缩放的起点
      endValue: 9, //区域缩放的终点
      timeId: null,
    };
  },
  created() {
    // console.log(this.$nextTick);
    //在组件创建完成后，进行回调函数的注册
    this.$socket.registerCallBack("rankData", this.getData);
  },
  mounted() {
    //dom元素挂载完成
    this.initChart();
    // this.getData();
    //发送数据给服务器,获取数据
    this.$socket.send({
      action: "getData",
      socketType: "rankData",
      chartName: "rank",
      value: "",
    });
    window.addEventListener("resize", this.screenAdapter);
    //在页面加载完成时,主动进行屏幕的适配
    this.screenAdapter();
  },
  destroyed() {
    //组件销毁时,取消监听器
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timeId);
    //在组件销毁后，进行回调函数的移除
    this.$socket.removeCallBack("rankData");
  },
  watch: {
    theme() {
      this.chartInstance.dispose(); //销毁当前图表
      this.initChart(); //重新初始化新主题的图表
      this.updateChart(); //更新图表
      this.screenAdapter(); //完成图表的适配
    },
  },
   computed: {
    ...mapState(["theme"]),
  },
  methods: {
    //初始化echarts
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.rank_ref, this.theme);
      //对图表初始化的配置
      const initOption = {
        title: {
          text: "▎地区销售分布",
          left: 20,
          top: 20,
        },
        //坐标轴
        grid: {
          top: "40%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true,
        },
        //工具提示
        tooltip: {
          show: true,
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        // series: [
        //   {
        //     type: "bar",
        //   },
        // ],
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    async getData(ret) {
      //获取数据,更新图表数据
      // const { data: ret } = await this.$http.get("/rank");
      this.allData = ret;
      //排序
      this.allData.sort((a, b) => {
        return b.value - a.value; //从大到小
      });
      // console.log(this.allData);
      this.updateChart();
      this.startInterval();
    },
    //更新图表
    updateChart() {
      //更新图表的配置
      //颜色渐变的处理
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"],
      ];
      //所有省份的数组
      const proviceArr = this.allData.map((item) => {
        return item.name;
      });
      //所有省份对应的销售金额
      const valueArr = this.allData.map((item) => {
        return item.value;
      });
      const dataOption = {
        xAxis: {
          data: proviceArr,
        },
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
        series: [
          {
            type:"bar",
            data: valueArr,
            itemStyle: {
              color: (arg) => {
                let targetColorArr = null;
                if (arg.value > 300) {
                  targetColorArr = colorArr[0];
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0],
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1],
                  },
                ]);
              },
            },
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    startInterval() {
      if (this.timeId) {
        clearInterval(this.timeId);
      }
      this.timeId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateChart();
      }, 2000);
    },
    //当浏览器的大小发生变化时,调用该函数,完成屏幕适配
    screenAdapter() {
      //图表屏幕分辨率的配置
      //获取图表容器的宽度
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.2;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series: [
          {
            type: "bar",
            barWidth: titleFontSize,
            itemStyle: {
              //柱形图圆角
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      //手动调用图表resize
      this.chartInstance.resize();
    },
  },
};
</script>

<style>
</style>
